<template>
  <div>
    <!-- 对于表单的校验
    一共需要三个数据绑定
    1、el-form 绑定model整体数据对象
    2、el-form 绑定rules规则定义
    3、el-form-item 绑定prop指定字段名字符串 -->
    <el-form ref="myForm" :model="formData" :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item>
        <el-button @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        mobile: '',
        password: ''
      },
      // 规则是个对象，里面每个属性都是要校验的字段，跟formData对应
      rules: {
        // 每个属性校验都是一个数组，可以接收多条规则对象
        mobile: [{
          required: true, message: '该项不能为空', trigger: 'blur'
        }, {
          pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确手机号码', trigger: 'blur'
        }
        ],
        password: [
          {
            required: true, message: '该项不能为空', trigger: 'blur'
          },
          {
            min: 3, max: 12, message: '密码必须在3-12位之间', trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.myForm.validate().then(() => {
        console.log(this.formData)
      })
    }
  }
}

</script>

<style>

</style>
